package com.meessiom.train.crm.RepaymentSchedule.controller;

import com.meessiom.train.crm.RepaymentSchedule.model.RepaymentScheduleInfo;
import com.meessiom.train.crm.RepaymentSchedule.service.RepaymentScheduleInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;


import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/api/v1/repayment_Schedules")
@Slf4j

public class RepaymentScheduleController {
    @Autowired
    private RepaymentScheduleInfoService repaymentScheduleInfoService;

    @GetMapping
    public List<RepaymentScheduleInfo> getAllRepaymentSchedules(){
        return repaymentScheduleInfoService.getAllRepaymentSchedules();
    }
    @PostMapping
    public RepaymentScheduleInfo createOrder(@RequestBody RepaymentScheduleInfo repaymentSchedule){
        return repaymentScheduleInfoService.createRepaymentSchedule(repaymentSchedule);
    }
    @DeleteMapping("{id}")
    public RepaymentScheduleInfo deleteRepaymentSchedule(@PathVariable long id){
        return repaymentScheduleInfoService.deleteRepaymentSchedule(id);
    }
    @PutMapping("{id}")
    public RepaymentScheduleInfo updateRepaymentSchedule(
            @PathVariable Long id,
            @RequestBody RepaymentScheduleInfo repaymentSchedule
    ) {
        log.info("Updating repayment schedule ID: {}, Data: {}", id, repaymentSchedule);
        return repaymentScheduleInfoService.updateRepaymentSchedule(id, repaymentSchedule);
    }
    @GetMapping("{id}")
    public RepaymentScheduleInfo getRepaymentScheduleById(@PathVariable long id) {
        return repaymentScheduleInfoService.getRepaymentScheduleById(id);
    }
    @PostMapping("/import")
    public ResponseEntity<?> importRepaymentSchedules(@RequestBody List<RepaymentScheduleInfo> repaymentSchedules) {
        try {
            List<RepaymentScheduleInfo> savedSchedules = repaymentScheduleInfoService.saveAll(repaymentSchedules);
            return ResponseEntity.ok(savedSchedules);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("导入失败: " + e.getMessage());
        }
    }

}
